package unote.views;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.part.ViewPart;

import unote.model.ArticleModel;
import unote.service.IArticleService;
import unote.views.provider.ArticleContentProvider;
import unote.views.provider.ArticleLabelProvider;

public class ArticleListView extends ViewPart {

	public static final String ID = "uNote.views.articleList";

	IArticleService service;

	TableViewer tableViewer = null;

	public ArticleListView() {
	}

	public ArticleListView(IArticleService service) {
		this.service = service;
	}

	@Override
	public void createPartControl(Composite parent) {
		tableViewer = new TableViewer(parent, SWT.BORDER | SWT.MULTI);

		Table table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn articleName = new TableColumn(table, SWT.NONE);
		articleName.setText("Name");
		articleName.setWidth(100);
		
		TableColumn description = new TableColumn(table, SWT.NONE);
		description.setText("Description");
		description.setWidth(150);

		TableColumn articleTag = new TableColumn(table, SWT.NONE);
		articleTag.setText("Tags");
		articleTag.setWidth(80);

		TableColumn articleUpdate = new TableColumn(table, SWT.NONE);
		articleUpdate.setText("Update");
		articleUpdate.setWidth(80);

		tableViewer.setContentProvider(new ArticleContentProvider());
		tableViewer.setLabelProvider(new ArticleLabelProvider());
		tableViewer.setInput(generateData());
		
		tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				ArticleModel model = (ArticleModel) selection.getFirstElement();
				
				MessageDialog.openInformation(null, "hello", model.getName());
			}
		});
	}

	@Override
	public void setFocus() {
	}

	private List<ArticleModel> generateData() {
		List<ArticleModel> result = new ArrayList<ArticleModel>();
		
		ArticleModel model1 = new ArticleModel();
		model1.setId(1);
		model1.setName("name1");
		model1.setDescription("ddddddddd1");
		model1.setTags("ttttttttt1");
		model1.setCreate(new Date());
		model1.setUpdate(new Date());
		
		ArticleModel model2 = new ArticleModel();
		model2.setId(2);
		model2.setName("name2");
		model2.setDescription("ddddddddd2");
		model2.setTags("ttttttttt1");
		model2.setCreate(new Date());
		model2.setUpdate(new Date());
		
		
		ArticleModel model3 = new ArticleModel();
		model3.setId(3);
		model3.setName("name3");
		model3.setDescription("ddddddddd3");
		model3.setTags("ttttttttt1");
		model3.setCreate(new Date());
		model3.setUpdate(new Date());

		result.add(model1);
		result.add(model2);
		result.add(model3);
		
		return result;
//		 List<ArticleModel> articles = service.queryArticles();
//		 for (ArticleModel model : articles) {
//		 TableItem item = new TableItem(table, SWT.NONE);
//		 item.setText(new String[] { model.getName(), model.getTags(),
//		 model.getUpdate().toString() });
//		 item.setForeground(red);
//		 }
		
	}

}
